package edu.pku.sei.modeler.neditor.command;

import org.eclipse.draw2d.geometry.Point;
import org.eclipse.gef.commands.Command;

import edu.pku.sei.modeler.model.notation.shape.ConnectionTemplate;
import edu.pku.sei.modeler.neditor.part.ConnectionEditPart;

public class ChangeLinePointCommand extends Command {
	private ConnectionEditPart editPart;

	private Point newPoint;

	private boolean isStartPoint;

	public void setEditPart(ConnectionEditPart editPart) {
		this.editPart = editPart;
	}

	public void setStartPoint(boolean isStartPoint) {
		this.isStartPoint = isStartPoint;
	}

	public void setNewPoint(Point newPoint) {
		this.newPoint = newPoint;
	}

	@Override
	public boolean canExecute() {
		return true;
	}

	private Point oldPoint = new Point();
	
	@Override
	public void execute() {
		ConnectionTemplate conn = (ConnectionTemplate) editPart.getModel();
		if (isStartPoint) {
			oldPoint.setLocation(conn.getStartPoint());
			conn.setStartPoint(newPoint.x, newPoint.y);
		} else {
			oldPoint.setLocation(conn.getEndPoint());
			conn.setEndPoint(newPoint.x, newPoint.y);
		}
	}

	@Override
	public void redo() {
		// TODO Auto-generated method stub
		ConnectionTemplate conn = (ConnectionTemplate) editPart.getModel();
		if (isStartPoint) {			
			conn.setStartPoint(newPoint.x, newPoint.y);
		} else {
			conn.setEndPoint(newPoint.x, newPoint.y);
		}
	}

	@Override
	public void undo() {
		// TODO Auto-generated method stub
		ConnectionTemplate conn = (ConnectionTemplate) editPart.getModel();
		if (isStartPoint) {			
			conn.setStartPoint(oldPoint.x, oldPoint.y);
		} else {			
			conn.setEndPoint(oldPoint.x, oldPoint.y);
		}
	}

}
